#coding=utf-8
#!/usr/bin/python

'''
Created on 2012-12-4

@author: Neo.Chen
'''

import socket,select,sys,time
import thread
import xml.dom.minidom
import os

#端口配置信息列表
g_config = []


def loop(cs,addr,s_ip,s_port):    
    print '%s %d connected.' % addr    
    ts = socket.socket()    
    
    try:    
        ts.connect((s_ip,s_port))    
    except:    
        cs.close()    
        print '%s %d closed.' % addr
        sys.exit(0)    
    
    while True:    
    
        rl,wl,xl = select.select([cs.fileno(),ts.fileno()],[],[cs.fileno(),ts.fileno()])    
    
        if len(xl) > 0:    
            cs.close()    
            ts.close()    
            print '%s %d closed.' % addr    
            sys.exit(0)    
    
        if len(rl) > 0:    
            if rl[0] == cs.fileno():    
                rs = ts    
                ws = cs    
            else:    
                rs = cs    
                ws = ts    
    
            try:    
                buffer = ws.recv(10000)    
                if len(buffer) == 0:    
                    raise    
                rs.send(buffer)    
            except:    
                rs.close()    
                ts.close()    
                print '%s %d closed.' % addr    
                sys.exit(0)    
    
def mainserver(l_port,s_ip,s_port):     
    try:    
        ss = socket.socket()
        ss.bind(('0.0.0.0',l_port))
        ss.listen(10)
    except:    
        sys.exit(0)
    
    while True:    
        cs,addr = ss.accept()
        thread.start_new_thread(loop,(cs,addr,s_ip,s_port))
        print "本地端口: %d" % l_port, "接收到新连接：%s:%d" % addr
    
def main():    
    if True :
        try:
            load_config()
            use_config()
            print "proxy starting..."
        except:
            pass
    
    while True:
        time.sleep(1)

'''
从配置文件加载端口参数
'''
def load_config():
    dom = xml.dom.minidom.parse(os.getcwd() + '/TransServer.xml')
    root = dom.documentElement
    mapping = root.getElementsByTagName('mapping')
    for node in mapping:
        listen_port = node.getElementsByTagName('listen_port')
        mapping_ip = node.getElementsByTagName('mapping_ip')
        mapping_port = node.getElementsByTagName('mapping_port')
        print listen_port[0].firstChild.nodeValue, \
                mapping_ip[0].firstChild.nodeValue, \
                mapping_port[0].firstChild.nodeValue
        #构造一个结构
        obj = []
        obj.append(listen_port[0].firstChild.nodeValue)
        obj.append(mapping_ip[0].firstChild.nodeValue)
        obj.append(mapping_port[0].firstChild.nodeValue)
        #加到配置
        g_config.append(obj)
    
    
'''
应用端口参数进行监听
'''
def use_config():
    for obj in g_config:
        listen_port = int(obj[0])
        mapping_ip = obj[1]
        mapping_port = int(obj[2])
        thread.start_new_thread(mainserver, (listen_port, mapping_ip, mapping_port))
        print "local:%d  ------> %s:%d" % (listen_port, mapping_ip, mapping_port)
        
'''
写日志到文件
'''

        
        
if __name__ == '__main__':
    #PrintLog()
    main()

    
    


























